#include "ctrlbar.h"
#include "ui_ctrlbar.h"
#include "globalresource.h"

CtrlBar::CtrlBar(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CtrlBar)
{
#ifdef  Debug_Falg
    qDebug() << "The name is : <" << __func__ << "> The file is :<" << __FILE__ << ">Thread is <" << QThread::currentThreadId();
#endif
    ui->setupUi(this);
    state_volume = false;
    Init();
    setStyleSheet("QToolTip{border: none;background-color: Cyan;color:white;}");
}

CtrlBar::~CtrlBar()
{
    delete ui;
}

CtrlBar::Init()
{
#ifdef  Debug_Falg
    qDebug() << "The name is : <" << __func__ << "> The file is :<" << __FILE__ << ">Thread is <" << QThread::currentThreadId();
#endif
    ui->PlayOrPauseBtn->setStyleSheet("background-color: rgba(255, 255, 255, 0);border:0px;");
    ui->StopBtn->setStyleSheet("background-color: rgba(255, 255, 255, 0);border:0px;");
    ui->VolumeBtn->setStyleSheet("background-color: rgba(255, 255, 255, 0);border:0px;");
    ui->ForwardBtn->setStyleSheet("background-color: rgba(255, 255, 255, 0);border:0px;");
    ui->BackwardBtn->setStyleSheet("background-color: rgba(255, 255, 255, 0);border:0px;");
    ui->speedBtn->setStyleSheet("background-color: rgba(255, 255, 255, 0);border:0px;");
    ui->VideoPlayTimeTimeEdit->setStyleSheet("background-color: rgba(255, 255, 255, 0);border:0px;");
    ui->VideoTotalTimeTimeEdit->setStyleSheet("background-color: rgba(255, 255, 255, 0);border:0px;");

    ui->addFile->setStyleSheet("background-color: rgba(255, 255, 255, 0);border: 2px solid red;");
    ui->addAddress->setStyleSheet("background-color: rgba(255, 255, 255, 0);border: 2px solid red;");
    ui->deleteThis->setStyleSheet("background-color: rgba(255, 255, 255, 0);border: 2px solid red;");
    ui->clearOut->setStyleSheet("background-color: rgba(255, 255, 255, 0);border: 2px solid red;");

    ui->PlayOrPauseBtn->setIcon(QIcon(":/Resources/MainWindow/VideoWindow/play.png"));
    ui->StopBtn->setIcon(QIcon(":/Resources/MainWindow/VideoWindow/end.png"));
    ui->ForwardBtn->setIcon(QIcon(":/Resources/MainWindow/VideoWindow/after.png"));
    ui->BackwardBtn->setIcon(QIcon(":/Resources/MainWindow/VideoWindow/before.png"));
    ui->VolumeBtn->setIcon(QIcon(":/Resources/MainWindow/VideoWindow/aac_open.png"));

    ui->PlayOrPauseBtn->setToolTip("播放");
    ui->StopBtn->setToolTip("终止");
    ui->BackwardBtn->setToolTip("上一个");
    ui->ForwardBtn->setToolTip("下一个");
    ui->speedBtn->setToolTip("倍速");



    ConnectSignalSlots();
}

bool CtrlBar::ConnectSignalSlots()
{
#ifdef  Debug_Falg
    qDebug() << "The name is : <" << __func__ << "> The file is :<" << __FILE__ << ">Thread is <" << QThread::currentThreadId();
#endif
    connect(ui->PlaySlider, &CustomSlider::SigCustomSliderValueChanged, this, &CtrlBar::SlotPlaySliderValueChanged);
    connect(ui->VolumeSlider, &CustomSlider::SigCustomSliderValueChanged, this, &CtrlBar::SlotVolumeSliderValueChanged);

    return true;
}

void CtrlBar::SlotPlaySliderValueChanged()
{
#ifdef  Debug_Falg
    qDebug() << "The name is : <" << __func__ << "> The file is :<" << __FILE__ << ">Thread is <" << QThread::currentThreadId();
#endif
    double dPercent = ui->PlaySlider->value()*1.0 / ui->PlaySlider->maximum();
    emit SigPlaySeek(dPercent);
}

void CtrlBar::SlotVolumeSliderValueChanged()
{
#ifdef  Debug_Falg
    qDebug() << "The name is : <" << __func__ << "> The file is :<" << __FILE__ << ">Thread is <" << QThread::currentThreadId();
#endif
    double dPercent = ui->VolumeSlider->value()*1.0 / ui->VolumeSlider->maximum();
    emit SigPlayVolume(dPercent);

    SlotVideoVolume(dPercent);
}

void CtrlBar::on_PlayOrPauseBtn_clicked()
{
#ifdef  Debug_Falg
    qDebug() << "The name is : <" << __func__ << "> The file is :<" << __FILE__ << ">Thread is <" << QThread::currentThreadId();
#endif
    emit SigPlayOrPause();
}

void CtrlBar::on_StopBtn_clicked()
{
#ifdef  Debug_Falg
    qDebug() << "The name is : <" << __func__ << "> The file is :<" << __FILE__ << ">Thread is <" << QThread::currentThreadId();
#endif
    emit SigStop();
}

void CtrlBar::on_BackwardBtn_clicked()
{
    emit SigBackwardPlay();
}

void CtrlBar::on_ForwardBtn_clicked()
{
    emit SigForwardPlay();
}

void CtrlBar::on_speedBtn_clicked()
{
#ifdef  Debug_Falg
    qDebug() << "The name is : <" << __func__ << "> The file is :<" << __FILE__ << ">Thread is <" << QThread::currentThreadId();
#endif
    // 设置变速
    emit SigSpeed();
}


/*
 * 喇叭按钮槽函数
*/
void CtrlBar::on_VolumeBtn_clicked()
{
#ifdef  Debug_Falg
    qDebug() << "The name is : <" << __func__ << "> The file is :<" << __FILE__ << ">Thread is <" << QThread::currentThreadId();
#endif
    if(state_volume)
    {
        // 将音量按钮图标设置为正常音量图标
        ui->VolumeBtn->setIcon(QIcon(":/Resources/MainWindow/VideoWindow/aac_open.png"));
        state_volume = false;
    }else
    {
        // 将音量按钮图标设置为静音图标
        ui->VolumeBtn->setIcon(QIcon(":/Resources/MainWindow/VideoWindow/aac_close.png"));
        state_volume = true;
    }
}

void CtrlBar::SlotPauseStatus(bool bPaused)
{
#ifdef  Debug_Falg
    qDebug() << "The name is : <" << __func__ << "> The file is :<" << __FILE__ << ">Thread is <" << QThread::currentThreadId();
#endif
    // 输出调试信息，显示当前调用的函数名以及视频的暂停状态
    qDebug() << "CtrlBar::SlotPauseStatus" << bPaused;

    // 判断视频是否处于暂停状态
    if (bPaused)
    {
        // 若视频已暂停，将播放/暂停按钮的图标设置为播放图标
        ui->PlayOrPauseBtn->setIcon(QIcon(":/Resources/MainWindow/VideoWindow/play.png"));
        // 将播放/暂停按钮的工具提示设置为“播放”
        ui->PlayOrPauseBtn->setToolTip("播放");
    }
    else
    {
        // 若视频正在播放，将播放/暂停按钮的图标设置为暂停图标
        ui->PlayOrPauseBtn->setIcon(QIcon(":/Resources/MainWindow/VideoWindow/stop.png"));
        // 将播放/暂停按钮的工具提示设置为“暂停”
        ui->PlayOrPauseBtn->setToolTip("暂停");
    }
}

/**
 * @brief 处理播放停止完成后的界面更新操作
 *
 * 当播放停止操作完成时，该函数会被调用，用于将播放进度条归零，
 * 并将总播放时间和当前播放时间显示重置为 0，同时将播放/暂停按钮的图标
 * 设置为播放图标，并更新其工具提示信息。
 */
void CtrlBar::SlotStopFinished()
{
#ifdef  Debug_Falg
    qDebug() << "The name is : <" << __func__ << "> The file is :<" << __FILE__ << ">Thread is <" << QThread::currentThreadId();
#endif
    // 将播放进度条的值设置为 0，表示播放进度归零
    ui->PlaySlider->setValue(0);
    // 创建一个表示 0 时 0 分 0 秒的 QTime 对象
    QTime StopTime(0, 0, 0);
    // 将总播放时间显示设置为 0
    ui->VideoTotalTimeTimeEdit->setTime(StopTime);
    // 将当前播放时间显示设置为 0
    ui->VideoPlayTimeTimeEdit->setTime(StopTime);
    // 将播放/暂停按钮的图标设置为播放图标
    ui->PlayOrPauseBtn->setIcon(QIcon(":/Resources/MainWindow/VideoWindow/play.png"));
    // 将播放/暂停按钮的工具提示设置为“播放”
    ui->PlayOrPauseBtn->setToolTip("播放");
}

void CtrlBar::SlotSpeed(float speed)
{
#ifdef  Debug_Falg
    qDebug() << "The name is : <" << __func__ << "> The file is :<" << __FILE__ << ">Thread is <" << QThread::currentThreadId();
#endif
    // 更新速度
    ui->speedBtn->setText(QString("倍速:%1").arg(speed));
}

void CtrlBar::SlotVideoTotalSeconds(int nSeconds)
{
#ifdef  Debug_Falg
    qDebug() << "The name is : <" << __func__ << "> The file is :<" << __FILE__ << ">Thread is <" << QThread::currentThreadId();
#endif
    m_nTotalPlaySeconds = nSeconds;

    int thh, tmm, tss;
    thh = nSeconds / 3600;
    tmm = (nSeconds % 3600) / 60;
    tss = (nSeconds % 60);
    QTime TotalTime(thh, tmm, tss);

    ui->VideoTotalTimeTimeEdit->setTime(TotalTime);
}

/**
* @brief 更新视频当前播放时间，并同步更新播放进度条
* @param nSeconds 视频当前播放的时长，单位为秒
 */
void CtrlBar::SlotVideoPlaySeconds(int nSeconds)
{
#ifdef  Debug_Falg
    qDebug() << "The name is : <" << __func__ << "> The file is :<" << __FILE__ << ">Thread is <" << QThread::currentThreadId();
#endif
    int thh, tmm, tss;
    thh = nSeconds / 3600;
    tmm = (nSeconds % 3600) / 60;
    tss = (nSeconds % 60);
    QTime TotalTime(thh, tmm, tss);

    ui->VideoPlayTimeTimeEdit->setTime(TotalTime);

    ui->PlaySlider->setValue(nSeconds * 1.0 / m_nTotalPlaySeconds * MAX_SLIDER_VALUE);
}

void CtrlBar::SlotVideoVolume(double dPercent)
{
#ifdef  Debug_Falg
    qDebug() << "The name is : <" << __func__ << "> The file is :<" << __FILE__ << ">Thread is <" << QThread::currentThreadId();
#endif
    // 根据传入的音量百分比设置音量滑块的值
    ui->VolumeSlider->setValue(dPercent * MAX_SLIDER_VALUE);
    // 记录最后一次的音量百分比
    m_dLastVolumePercent = dPercent;

    // 判断音量是否为 0
    if (m_dLastVolumePercent == 0)
    {
        // 若音量为 0，将音量按钮图标设置为静音图标
        ui->VolumeBtn->setIcon(QIcon(":/Resources/MainWindow/VideoWindow/aac_close.png"));

    }
    else
    {
        // 若音量不为 0，将音量按钮图标设置为正常音量图标
        ui->VolumeBtn->setIcon(QIcon(":/Resources/MainWindow/VideoWindow/aac_open.png"));

    }

    // 将当前音量百分比保存到全局设置中
    GlobalResource::SavePlayVolume(dPercent);
}



void CtrlBar::on_addFile_clicked()
{
    emit AddFile();
}

void CtrlBar::on_addAddress_clicked()
{
    emit AddUrl();
}

void CtrlBar::on_deleteThis_clicked()
{
    emit RemoveFile();
}

void CtrlBar::on_clearOut_clicked()
{
    emit clear();
}
